package top.zywork.job;

import lombok.extern.slf4j.Slf4j;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.beans.factory.annotation.Autowired;
import top.zywork.annotation.ExposeClass;
import top.zywork.common.DateFormatUtils;
import top.zywork.common.DateParseUtils;
import top.zywork.common.DateUtils;
import top.zywork.dos.StatisticsDauDO;
import top.zywork.enums.DatePatternEnum;
import top.zywork.security.JwtTokenRedisUtils;
import top.zywork.service.ActivityService;
import top.zywork.service.StatisticsDauService;
import top.zywork.vo.ActivityVO;

import java.time.LocalDateTime;
import java.util.List;

/**
 * 更新活动状态作业<br/>
 *
 * 创建于2019-01-17<br/>
 *
 * @author 危锦辉
 * @version 1.0
 */
@ExposeClass(type = "job")
@Slf4j
public class ActivityStatusJob implements Job {

    private ActivityService activityService;

    @Override
    public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {
        log.info("begin to execute ActivityStatusJob......");
        List<ActivityVO> activityVOList = activityService.getAllActivity();
        String currDate = DateFormatUtils.format(DateUtils.currentDate(), DatePatternEnum.DATETIME.getValue());
        for (ActivityVO activityVO : activityVOList) {
            String startDate = activityVO.getStartDate();
            int compTo = currDate.compareTo(startDate);
            if (compTo > -1) {
                // 过期了需要更新数据库
                ActivityVO updateActivityVO = ActivityVO.builder().id(activityVO.getId()).status("已结束").build();
                activityService.update(updateActivityVO);
            }
        }

        log.info("executed ActivityStatusJob......");
    }

    @Autowired
    public void setActivityService(ActivityService activityService) {
        this.activityService = activityService;
    }
}
